package com.test.cofig;

import feign.Client;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 *  这玩意默认是关闭 的 防止不小心上生产！ 打开方式为 ： -Dopen.TrafficRedrect=1
 */
@Configuration
@ConditionalOnProperty(name="open.TrafficRedrect",matchIfMissing = false ,havingValue = "1")
public class TrafficRedrectConfig implements WebMvcConfigurer {

    @Bean
    @ConditionalOnProperty(name="open.TrafficRedrect",matchIfMissing = false ,havingValue = "1")
    public TrafficRedrectHandlerInterceptor trafficRedrectHandlerInterceptor(){
        return new TrafficRedrectHandlerInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {// 注册路由收集器
        registry.addInterceptor(trafficRedrectHandlerInterceptor()).addPathPatterns("/**");
    }


    @Bean @ConditionalOnProperty(name="open.TrafficRedrect",matchIfMissing = false ,havingValue = "1")
    public MyFeignTrafficRedrectClient myFeignTrafficRedrectClient( DiscoveryClient ds){
        return new MyFeignTrafficRedrectClient(new Client.Default(null,null),ds);
    }

}
